package com.example.farmer.Fragment.Class;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.farmer.Adapter.Class.CircleNew_PostClassAdapter;
import com.example.farmer.Adapter.Class.News_PostClassAdapter;
import com.example.farmer.Database.ClassPostDatabase;
import com.example.farmer.Entity.ClassPost;
import com.example.farmer.R;

import java.util.ArrayList;
import java.util.List;

public class PosterCircleNewsFragment extends Fragment {
    private List<String> posterCircleNewsList;
    private RecyclerView recyclerView;
    private TextView notNews;
    private CircleNew_PostClassAdapter adapter;
    private ClassPostDatabase post_db;
    private List<ClassPost> classPosts;
    private final MutableLiveData<List<ClassPost>> newsLiveData= new MutableLiveData<>();

    public static PosterCircleNewsFragment newInstance(List<String> posterCircleNewsList) {
        PosterCircleNewsFragment fragment = new PosterCircleNewsFragment();
        Bundle args = new Bundle();
        args.putStringArrayList("newsList", (ArrayList<String>) posterCircleNewsList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            posterCircleNewsList = getArguments().getStringArrayList("posterCircleNewsList");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.class_fragment_news, container, false);
        notNews = view.findViewById(R.id.not_news);

        recyclerView = view.findViewById(R.id.recyclerView_post);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化数据库并获取所有帖子
        post_db = ClassPostDatabase.getInstance(getContext());
        classPosts = post_db.getPostsByType("海报圈新闻");

        // 这里可以设置Adapter来显示列表数据
        adapter = new CircleNew_PostClassAdapter(classPosts, getContext());
        recyclerView.setAdapter(adapter);

        newsLiveData.observe(getViewLifecycleOwner(), this::updateUiWithPosts);
        refreshNewsData();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment可见时刷新数据
        refreshNewsData();
    }
    private void refreshNewsData() {
        // 从数据库获取最新数据
        classPosts = post_db.getPostsByType("海报圈新闻");
        // 更新LiveData的值
        newsLiveData.setValue(new ArrayList<>(classPosts));
    }
    private void updateUiWithPosts(List<ClassPost> classPosts) {
        if (classPosts != null && !classPosts.isEmpty()) {
            adapter.updatePosts(classPosts);
            notNews.setVisibility(View.GONE); // 假设没有数据时显示notNews
        } else {
            notNews.setText("没有最新内容哦");
            notNews.setVisibility(View.VISIBLE);
        }
    }
}